package com.lyz.moil.core.security.action;

import com.lyz.moil.core.orm.PageData;
import com.lyz.moil.core.utils.WebUtils;
import com.lyz.moil.core.security.entity.Department;
import com.lyz.moil.core.security.entity.DepartmentType;
import com.lyz.moil.core.security.service.DepartmentService;
import com.lyz.moil.core.security.service.DepartmentTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/security/department")
public class DepartmentAction {
    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private DepartmentTypeService departmentTypeService;

    @RequestMapping("/list")
    public ModelMap list(HttpServletRequest request) {
        PageData<Department> pageData = new PageData<Department>();
        // 给pageData设置参数
        WebUtils.setPageDataParameter(request, pageData);
        pageData = departmentService.find(pageData);

        return new ModelMap(pageData);
    }

    @RequestMapping(value = "/new")
    public String addForm(Model model) {
        common(model);
        model.addAttribute(new Department());
        return "/security/department/input";
    }

    @RequestMapping(value = "/edit/{id}")
    public String editForm(@PathVariable("id") Integer id, Model model) {
        Department department = departmentService.find(id);
        model.addAttribute(department);
        common(model);
        return "/security/department/input";
    }

    private void common(Model model) {
        List<DepartmentType> typeList = departmentTypeService.findAll();
        model.addAttribute("typeList", typeList);
        List<Department> departmentList = departmentService.findAll();
        model.addAttribute("departmentList", departmentList);
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String save(Department department, HttpServletRequest request) {
        if (department.isNew()) {
            departmentService.save(department);
        } else {
            departmentService.update(department);
        }
        return "redirect:/security/department/list";// 重定向
    }

    @RequestMapping(value = "/delete/{id}")
    public String delete(@PathVariable("id") Integer id) {
        departmentService.delete(id);
        return "redirect:/security/department/list";// 重定向
    }

    @RequestMapping(value = "/visible/{id}")
    public String visible(@PathVariable("id") Integer id) {
        departmentService.visible(id);
        return "redirect:/security/department/list";// 重定向
    }

    @RequestMapping(value = "/unVisible/{id}")
    public String unVisible(@PathVariable("id") Integer id) {
        departmentService.unVisible(id);
        return "redirect:/security/department/list";// 重定向
    }

    @InitBinder
    public void InitBinder(WebDataBinder dataBinder) {
        dataBinder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), false));
    }

}
